package com.embhealth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.embhealth.service.OrderSettingService;
import com.embhealth.constant.MessageConstant;
import com.embhealth.entity.Result;
import com.embhealth.pojo.OrderSetting;
import com.embhealth.service.OrderSettingService;
import com.embhealth.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约设置管理
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    //远程调用
    @Reference
    OrderSettingService orderSettingService;


    /**
     * 文件上传,实现预约设置数据批量导入
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        try {
            //使用POI解析表格数据,每一行的所有数据作为一个数组对象存到list集合中
            List<String[]> tableDataList = POIUtils.readExcel(excelFile);
            //创建预约设置的list集合
            List<OrderSetting> orderSettings = new ArrayList<>();
            //遍历表格数据,并将相应数据存储到list集合
            for (String[] strings : tableDataList) {
                //获取表格中预约设置日期数据
                String orderDate = strings[0];
                //获取表格中可预约人数数据
                String number = strings[1];
                //创建预约设置对象
                OrderSetting orderSetting = new OrderSetting(new Date(orderDate), Integer.parseInt(number));
                //System.out.println(new Date(orderDate));
                //存储到预约设置的list集合
                orderSettings.add(orderSetting);
            }
            //通过dubbo远程调用服务实现数据批量导入到数据库
            orderSettingService.add(orderSettings);
            //数据导入成功
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            //表示数据导入失败
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据月份查询对应的预约设置数据
     * @param date
     * @return
     */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据当前日期编辑可预约人数
     * @param date
     * @return
     */
    @RequestMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByOrderDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
}